package com.devcon5.StudentPlanner;

import java.util.Calendar;

import baseclasses.Course;
import database.CourseDAO;
import forms.CourseForm;

import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Toast;

public class EditCourse extends CourseForm {

	private Course course;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		int id = getIntent().getExtras().getInt("courseid");
		course = CourseDAO.getCourseById(context, id);
		
		if(savedInstanceState==null)
		{
			//initialize weekday booleans
			weekDayBooleans = course.getWeekDayBooleans();
			//initialize calendar
			startTimeCalendar = course.getStartTime();
			//set widgets to contain course variables
			courseNameEditText.setText(course.getName());
			courseDaysText.setText(course.getWeekDays());
			courseTimeText.setText(course.getStartTimeString("hh:mm a"));
			courseCreditsEditText.setText(course.getCredits()+"");
			courseLocationEditText.setText(course.getLocation());
			
			endTimeCalendar = course.getEndTime();	//get the end time that class is over
			
			if( endTimeCalendar == null )	//if there is no end time set
			{
				long time = startTimeCalendar.getTimeInMillis();
				time = time + (1000*60*50);	//set the end time = start time + 50 minutes
				endTimeCalendar = Calendar.getInstance();
				endTimeCalendar.setTimeInMillis(time);
				course.setEndTime(endTimeCalendar);
			}
			courseTimeText.setText(course.getEndTimeString("hh:mm a"));
		}
		
		courseTimeText.setText(Course.getStartTimeString(startTimeCalendar, "hh:mm a"));
		courseEndTimeText.setText(Course.getEndTimeString(endTimeCalendar, "hh:mm a"));
		courseDaysText.setText(Course.getWeekDays(weekDayBooleans));

		saveButton.setText("Save");

		cancelButton.setOnClickListener(new OnClickListener(){
			
			public void onClick(View v) {
				onBackPressed();
				finish();
			}
		});

	}

	@Override
	protected void onCourseWeekDaysLayoutClicked() {
		super.onCourseWeekDaysLayoutClicked();
		course.setWeekDayBooleans(weekDayBooleans);
	}

	@Override
	protected void onCourseTimeLayoutClicked(View v) {
		super.onCourseTimeLayoutClicked(v);
		course.setStartTime(startTimeCalendar);
	}
	
	@Override
	protected void onCourseEndTimeLayoutClicked(View v) {
		super.onCourseEndTimeLayoutClicked(v);
		course.setEndTime(endTimeCalendar);
	}
	
	@Override
	protected void onSaveButtonClicked() {
		course.setName(courseNameEditText.getText().toString());
		course.setLocation(courseLocationEditText.getText().toString());
		int credits;
		try{
			credits = Integer.parseInt(courseCreditsEditText.getText().toString());
		}
		catch(java.lang.NumberFormatException e){
			credits = 3;
		}
		course.setCredits(credits);
		//if course name is valid (not blank) proceed
		if(courseNameValid(course.getName())){


			//if weekdays are valid (at least one checked) proceed
			if(weekDaysValid(course.getWeekDayBooleans())){

				//if all input is valid create course object and add to database

				if(CourseDAO.editCourse(context, course)==-1){
					Toast.makeText(getBaseContext(), "Error adding course", Toast.LENGTH_LONG).show();
				}
				else{
					
					onBackPressed();
					finish();
				}
			}

			//if weekday is not valid (nothing selected) show message
			else{
				Toast.makeText(getBaseContext(), "You must select atleast 1 weekday!", Toast.LENGTH_LONG).show();
			}
		}
		//if course name is not valid show message
		else{
			Toast.makeText(getBaseContext(),  "Course title cannot be blank!", Toast.LENGTH_LONG).show();
		}
	}
}